package com.hb.trade.api.routine.controller.expert;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.hb.core.pojo.expert.dto.*;
import com.hb.core.pojo.expert.entity.ExpertGroupMembers;
import com.hb.trade.client.expert.ExpertClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.MsgUtil;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专家资料库API控制器
 *
 * @author ljs
 * @date 2022-2-10 16:32:04
 */
@Slf4j
@Version(1.0)
@Api(tags = {"专家资料库"})
@RequestMapping("/{version}")
@RestController
public class ApiExpertController {
    protected ExpertClient expertClient;

    public ApiExpertController(ExpertClient expertClient) {
        this.expertClient = expertClient;
    }

    @ApiOperation(value = "抽取专家", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "majorAmountList", value = "专业以及数量", dataTypeClass = MajorAmount.class)
    })
    @PostMapping("/expert/expertExtract")
    public ResponseDto expertExtract(@RequestBody ExpertExtractParam expertExtractParam) {
        return ResultUtil.msg(expertClient.expertExtract(expertExtractParam));
    }

    @ApiOperation(value = "根据父类code获取专业列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentCode", value = "父类Code", dataTypeClass = String.class)
    })
    @PostMapping("/expert/majorList")
    public ResponseDto majorList(@RequestBody ExpertMajorDTO expertMajorDTO) {
        Message result = MsgUtil.success(expertClient.majorList(expertMajorDTO));
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "获取全部专业列表", httpMethod = "POST")
    @ApiImplicitParams({
    })
    @PostMapping("/expert/allMajorList")
    public ResponseDto allMajorList(@RequestBody ExpertMajorDTO expertMajorDTO) {
        Message result = MsgUtil.success(expertClient.majorList(expertMajorDTO));
        return ResultUtil.msg(result);
    }


    @ApiOperation(value = "分页搜索专家", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchParam", value = "搜索参数", dataTypeClass = String.class),
            @ApiImplicitParam(name = "majorName", value = "专业名称", dataTypeClass = String.class)
    })
    @PostMapping("/expert/searchExpert")
    public ResponseDto expertSearch(@RequestBody ExpertDTO expertDTO) {
        Message result = MsgUtil.success(expertClient.expertSearch(expertDTO));
        return ResultUtil.msg(result);
    }
    @ApiOperation(value = "搜索全部专家", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchParam", value = "搜索参数", dataTypeClass = String.class),
            @ApiImplicitParam(name = "majorName", value = "专业名称", dataTypeClass = String.class)
    })
    @PostMapping("/expert/expertAll")
    public ResponseDto expertAll(@RequestBody ExpertDTO expertDTO) {
        Message result = expertClient.expertAll(expertDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "添加专家", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expertName", value = "专家姓名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "idCard", value = "专家身份证号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sex", value = "性别（0，男；1，女）", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "phoneNumber", value = "手机", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "majorCode", value = "专业编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "onworkFlag", value = "是否在职（1：在职，0：不在职）", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "company", value = "工作单位", dataTypeClass = String.class)
    })
    @PostMapping("/expert/expertSave")
    public ResponseDto expertSave(@RequestBody ExpertDTO expertDTO) {
        Message result = expertClient.expertSave(expertDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "更新专家信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "专家ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "expertName", value = "专家姓名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "idCard", value = "专家身份证号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sex", value = "性别（0，男；1，女）", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "phoneNumber", value = "手机", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "majorCode", value = "专业编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "onworkFlag", value = "是否在职（1：在职，0：不在职）", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "company", value = "工作单位", dataTypeClass = String.class)
    })
    @PostMapping("/expert/expertUpdate")
    public ResponseDto expertUpdate(@RequestBody ExpertDTO expertDTO) {
        Message result = expertClient.expertUpdate(expertDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "删除专家信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "专家ID", dataTypeClass = Long.class)
    })
    @PostMapping("/expert/expertDelete")
    public ResponseDto expertDelete(@RequestBody ExpertDTO expertDTO) {
        Message result = expertClient.expertDelete(expertDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "保存专家组信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expertList", value = "专家信息列表", dataTypeClass = ExpertGroupMembers.class),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = String.class)
    })
    @PostMapping("/expert/expertGroupSave")
    public ResponseDto expertGroupSave(@RequestBody ExpertGroupDTO expertGroupDTO) {
        Message result = expertClient.expertGroupSave(expertGroupDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "查询当前用户下专家组", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = ExpertGroupMembers.class)
    })
    @PostMapping("/expert/expertGroupList")
    public ResponseDto expertGroupList(@RequestBody ExpertGroupDTO expertGroupDTO){
        Message result = expertClient.expertGroupList(expertGroupDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "查询专家组详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "专家组ID", dataTypeClass = ExpertGroupMembers.class)
    })
    @PostMapping("/expert/expertGroupDetail")
    public ResponseDto expertGroupDetail(@RequestBody ExpertGroupDTO expertGroupDTO){
        Message result = expertClient.expertGroupDetail(expertGroupDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "删除专家组", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataTypeClass = ExpertGroupMembers.class)
    })
    @PostMapping("/expert/expertGroupDelete")
    public ResponseDto expertGroupDelete(@RequestBody ExpertGroupDTO expertGroupDTO){
        Message result = expertClient.expertGroupDelete(expertGroupDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "专家列表导入专家", notes = "专家列表导入专家", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象", dataTypeClass = MultipartFile.class),
    })
    @PostMapping("/expert/importExcel")
    public ResponseDto importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream(), 0);
        reader.addHeaderAlias("专家姓名", "expertName");
        reader.addHeaderAlias("身份证号", "idCard");
        reader.addHeaderAlias("性别", "sexStr");
        reader.addHeaderAlias("联系方式", "phoneNumber");
        reader.addHeaderAlias("专业类别", "majorName");
        reader.addHeaderAlias("工作单位", "company");
        reader.addHeaderAlias("是否在职", "onWork");

        List<ExpertDTO> read = reader.readAll(ExpertDTO.class);
        if (CollUtil.isEmpty(read)) {
            return ResultUtil.msg(true, read);
        }
        //过滤空值
        read = read.stream().filter(expert -> StrUtil.isNotEmpty(expert.getExpertName())).collect(Collectors.toList());

        int size = read.size();
        for (int i = 0; i < size; i++) {
            String msg = "第" + (i + 2) + "行";
            ExpertDTO expertDTO = read.get(i);
            if (StrUtil.isEmpty(expertDTO.getExpertName())) {
                return ResultUtil.fail(msg + "专家姓名必填");
            }

            if (expertDTO.getIdCard() == null) {
                return ResultUtil.fail(msg + "身份证号必填");
            }
            if (expertDTO.getPhoneNumber() == null) {
                return ResultUtil.fail(msg + "联系方式必填");
            }
            if (expertDTO.getMajorName() == null) {
                return ResultUtil.fail(msg + "专业类别必填");
            }
            if (expertDTO.getOnWork() == null) {
                return ResultUtil.fail(msg + "是否在职必填");
            }

        }
        Message result = expertClient.expertImport(read);
        if (result.isFail()){
            return ResultUtil.fail(result.getMessage());
        }

        return ResultUtil.msg(true, read);
    }

    @ApiOperation(value = "抽取专家短信模板列表", httpMethod = "POST")
    @PostMapping("/expert/smstpls")
    public ResponseDto smstpls(){
        JSONObject obj = JSONUtil.createObj();
        obj.putOpt("SMS_234397194", "专家您好，您被抽取到于${time}在${address}评标，回复1确认，回复0拒绝。");
        return ResultUtil.msg(true, CollUtil.list(false, obj));
    }
}
